package com.puppet.building.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.puppet.building.pojo.House;
import com.puppet.building.pojo.MyPageData;
import com.puppet.building.pojo.Result;
import com.puppet.building.pojo.dto.HouseDTO;
import com.puppet.building.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/house")
public class HouseController {

    @Autowired
    private HouseService houseService;

    @GetMapping("/getHouseNoAnId")
    public Result<?> getHouseNoAnId() {
        return Result.ok(houseService.getHouseNoAnId());
    }

    @PostMapping("/{pageNum}/{pageSize}")
    public Result<?> getAllHouse(
            @PathVariable Integer pageNum,
            @PathVariable Integer pageSize,
            @RequestBody(required = false) HouseDTO houseDTO
    ) {
        PageHelper.startPage(pageNum, pageSize);
        List<House> allHouse = houseService.getAllHouse(houseDTO);
        PageInfo<House> pageInfo = new PageInfo<>(allHouse);
        MyPageData myPageData = new MyPageData(pageInfo.getList(), pageInfo.getTotal());
        return Result.ok(myPageData);
    }
    @GetMapping("/getHouseAnState")
    public Result<?>getHouseAnState(){
        return Result.ok(houseService.getHouseAnState());
    }
}
